package com.example.androidlearning.sy11.sy2dict;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.androidlearning.R;

import java.util.ArrayList;
import java.util.Map;

public class ResultActivity extends AppCompatActivity {

    ContentResolver contentResolver;
    private ListView show;
    SimpleAdapter arrayAdapter;
    ArrayList<Map<String, String>> result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sy1102_result);
        setTitle("单词");

        init();
        setListener();
    }

    private void init(){
        contentResolver = getContentResolver();
        show = findViewById(R.id.sy11_listview);

        Intent intent = getIntent();
        Bundle data = intent.getExtras();
        result = (ArrayList<Map<String, String>>) data.getSerializable("data");

        arrayAdapter = new SimpleAdapter(this,
                result,
                R.layout.activity_sy1102_item, new String[]{Words.Word.WORD, Words.Word.DETAIL},
                new int[]{R.id.sy11_resutlword, R.id.sy11_resutldetail});

        show.setAdapter(arrayAdapter);
    }

    private void setListener(){

        show.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(final AdapterView<?> parent, final View view, final int position, long id) {
                AlertDialog.Builder ab = new AlertDialog.Builder(getContext());
                ab.setTitle("确认");
                ab.setMessage("是否删除该单词");
                ab.setPositiveButton("确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(delete(Integer.parseInt(result.get(position).get(Words.Word._ID)))){
                            Toast.makeText(getContext(), "删除成功", Toast.LENGTH_LONG).show();
                            result.remove(position);
                            arrayAdapter.notifyDataSetChanged();
                            show.setAdapter(arrayAdapter);
                        }
                    }
                });
                ab.show();
                return false;
            }
        });

    }

    private boolean delete(int key){
        Uri uri = ContentUris.withAppendedId(Words.Word.WORD_CONTENT_URI, key);
        int num = contentResolver.delete(uri, null, null);
        return num != 0;
    }

    private Context getContext(){
        return this;
    }


}
